package com.yinhe.music.yhmusic.album;

import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.common.GlideHelper.GlideHelper;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.base.IBaseModel;
import com.yinhe.music.yhmusic.collect.CollectContract;
import com.yinhe.music.yhmusic.collect.CollectPresenter;
import com.yinhe.music.yhmusic.model.AlbumInfo;
import com.yinhe.music.yhmusic.utils.DataHolder;
import com.yinhe.music.yhmusic.utils.ToastUtils;
import com.yinhe.music.yhmusic.widget.DividerItemDecoration;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hasee on 2017/5/11.
 */

public class AlbumManagerActivity extends BaseServiceActivity implements View.OnClickListener,CollectContract.ICollectView{
    private AlbumAdapter mAdapter;
    private List<AlbumInfo> list = new ArrayList<>();
    private List<AlbumInfo> deleteList = new ArrayList<>();
    private String ids;

    CollectPresenter<IBaseModel, CollectContract.ICollectView> collectPresenter;

    @Override
    public void initView() {
        RxBus.get().register(this);
        list = DataHolder.getInstance().getData();
        setToolbar("批量操作");
        initBottomMenu();
        setmRecyclerView();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_manage;
    }

    @Override
    public BasePresenter getPresenter() {
        return collectPresenter;
    }

    @Override
    public void initPresenter() {
        collectPresenter = new CollectPresenter<>();
        collectPresenter.attachModelView(new BaseModel(), this);
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }

    private void initBottomMenu() {
        TextView play = findViewById(R.id.play);
        TextView down = findViewById(R.id.down);
        TextView addTo = findViewById(R.id.add_to);
        TextView delete = findViewById(R.id.delete);
        play.setVisibility(View.GONE);
        down.setVisibility(View.GONE);
        addTo.setVisibility(View.GONE);
        delete.setOnClickListener(this);
    }


    private void setmRecyclerView() {
        RecyclerView mRecyclerView = findViewById(R.id.recyclerView);
        WrapContentLinearLayoutManager linearLayoutManager = new WrapContentLinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new AlbumAdapter(list);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST, 0, true));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.delete) setDeleteList();
    }

    private void setDeleteList() {
        deleteList.clear();
        StringBuilder albumIds = new StringBuilder();
        List<AlbumInfo> albumInfoList = mAdapter.getData();
        for (AlbumInfo albumInfo : albumInfoList) {
            if (albumInfo.isSelect()) {
                albumIds.append(albumInfo.getAlbumId()).append(",");
                deleteList.add(albumInfo);
            }
        }

        if (albumIds.length() > 0) {
            albumIds = new StringBuilder(albumIds.substring(0, albumIds.length() - 1));
            ids = albumIds.toString();
            showDeleteDialog();
        }
    }

    private void showDeleteDialog() {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("提示");
        dialog.setMessage("是否删除");
        dialog.setPositiveButton("确定", (dialogInterface, i) -> {});
        dialog.setNegativeButton("取消", null);
        dialog.show();
    }

    private void removeAlbumItem(List<AlbumInfo> list) {
        if (list != null && !list.isEmpty()) {
            int index = 0;
            for (AlbumInfo i : list) {
                for(AlbumInfo j : deleteList){
                    if (i.getAlbumId() == j.getAlbumId()) {
                        mAdapter.remove(index);
                    }
                }
                index++;
            }
        }
    }

    @Override
    public void updateCollectUI() {
        ToastUtils.show("删除成功");
        removeAlbumItem(list);
        RxBus.get().post(RxBusEventType.Album.COLLECT_ALBUM, ids);
    }

    private class AlbumAdapter extends BaseQuickAdapter<AlbumInfo, BaseViewHolder> {

        AlbumAdapter(@Nullable List<AlbumInfo> data) {
            super(R.layout.fragment_menu_manager, data);
        }

        @Override
        protected void convert(final BaseViewHolder helper, final AlbumInfo item) {
            helper.setText(R.id.menu_name, item.getAlbumName())
                    .setText(R.id.menu_sum, item.getSongsnum() + "首 " + "来自" + item.getSingerName());

            ImageView image = helper.getView(R.id.song_menu_img);
            GlideHelper.setImageResource(image, item.getImage(), R.drawable.default_cover);

            helper.itemView.setOnClickListener((v) -> {
                if (item.isSelect()) {
                    item.setSelect(false);
                    helper.setChecked(R.id.checkbox, false);
                } else {
                    item.setSelect(true);
                    helper.setChecked(R.id.checkbox, true);
                }
            });

            CheckBox checkBox = helper.getView(R.id.checkbox);
            checkBox.setOnCheckedChangeListener((compoundButton, b) ->
                item.setSelect(b)
            );
            checkBox.setChecked(item.isSelect());
        }
    }

}
